package cn.com.jslh.boarpro.main.presenter;

import android.app.Activity;
import android.content.Context;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;

import org.json.JSONException;

import cn.com.jslh.boarpro.main.MainActivity;
import cn.com.jslh.boarpro.main.model.LoginModelImpl;
import cn.com.jslh.boarpro.main.view.view.LoginView;
import cn.com.jslh.boarpro.util.WebService;

/**
 * 作者：周洲 on 2017/7/12 11:00
 * <p>
 * 邮箱：double0zhou@126.com
 */

public class LoginPresenterImpl {
    private Context context;
    private LoginView loginView;
    private LoginModelImpl loginModel;
    public static String USERNAME = "USERNAME";
    public static String PASSWORD = "PASSWORD";

    public LoginPresenterImpl(Context context, LoginView loginView) {
        this.context = context;
        this.loginView = loginView;
        loginModel = new LoginModelImpl(context);
    }

    /**
     * 记住密码 0  用户名 1 密码
     */
    public void rember(boolean isRenember, String... params) {
        if (isRenember) { //记忆
            SPUtils.getInstance().put(USERNAME, params[0]);
            SPUtils.getInstance().put(PASSWORD, params[1]);
        } else {
            SPUtils.getInstance().remove(USERNAME);
            SPUtils.getInstance().remove(PASSWORD);
        }
    }

    public void initUserAndPass() {
        //获取本地存储的用户名密码，初始化
        this.loginView.initUserAndPass(SPUtils.getInstance().getString(USERNAME, ""), SPUtils.getInstance().getString(PASSWORD, ""));
    }

    /**
     * @param params 0  用户名  1 密码
     */
    public void doLogin(final String... params) {
        loginModel.doLogin(new WebService.NetSuccessListener() {
            @Override
            public void Success(String s) throws JSONException {
                ActivityUtils.startActivity((Activity)context, MainActivity.class);
                ((Activity)context).finish();
            }
        },params);
    }
}
